package com.prototype.ECE492;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapItem extends ItemizedOverlay<OverlayItem>
{
	private ArrayList<Tag> mOverlays = new ArrayList<Tag>();
	private ArrayList<Tag> mOverlaysBackup = new ArrayList<Tag>();
	private LinkedList<TagType> tagsOut = new LinkedList<TagType>();
	private Context mContext;
	public MapItem(Drawable defaultMarker, Context context)
	{
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		populate();
	}
	public void addOverlay(Tag overlay)
	{
		int i;
		if (tagsOut.contains(overlay.getType())){
			mOverlaysBackup.add(overlay);
//			populate();
			return;
		}
		if(mOverlays.size() != 0){
			for(i = 0; i < mOverlays.size();){
				if(mOverlays.get(i).getPoint().getLatitudeE6() != overlay.getPoint().getLatitudeE6())i++;
				else if(mOverlays.get(i).getPoint().getLongitudeE6() == overlay.getPoint().getLongitudeE6())break;
				else i++;
			}
			if(i >= mOverlays.size()){
				mOverlaysBackup.add(overlay);
				mOverlays.add(overlay);
			}
		}
		else{
			mOverlaysBackup.add(overlay);
			mOverlays.add(overlay);
		}
		populate();
	}
	
	public void filterOutType(TagType type){
		if (!tagsOut.contains(type)){
			tagsOut.add(type);
		}
	}
	public void filterInType(TagType type){
		if (tagsOut.contains(type)){
			tagsOut.remove(type);
		}
	}

/*	public void filter_list(Drawable marker, int filTest, boolean state){
		//New iteration filter_list if not equal
		if(this.filTest != filTest){
			System.out.println("Filter test not equal resetting states");
			mOverlays_filtered = new ArrayList<OverlayItem>();
			mOverlays = new ArrayList<OverlayItem>();
			System.out.println("Resetting to original list");
			for(int i = 0; i < mOverlays_backup.size(); i++){
				mOverlays.add(mOverlays_backup.get(i));
				populate();
			}
			this.filTest = filTest;
		}
		if(state)return;
		else{
			//mOverlays_filtered filters mOverlays
			System.out.println("Replacing with filtered list");
			for(int i = 0; i < mOverlays.size();i++){
				if(mOverlays.get(i).getMarker(0) != marker){
					mOverlays_filtered.add(mOverlays.get(i));
					for(int j = 0; j < mOverlays_filtered.size();j++){
						if(mOverlays_filtered.get(j).getMarker(0)==marker)mOverlays_filtered.remove(j);
					}
				}
			}
			mOverlays = new ArrayList<OverlayItem>();
			//mOverlays = mOverlays_filtered
			for(int i = 0; i < mOverlays_filtered.size();i++){
				mOverlays.add(mOverlays_filtered.get(i));
				populate();
			}
		}
		return;
	}*/
	public void filterList(){
		mOverlays = new ArrayList<Tag>();
		for (Tag check : mOverlaysBackup){
			if (!tagsOut.contains(check.getType())){
				mOverlays.add(check);
			}
		}
		populate();
	}
/*	private void filter_helper(Drawable marker, ArrayList<OverlayItem> OverlaysTo,
												ArrayList<OverlayItem> OverlaysFrom){
		ArrayList<OverlayItem> toRemove = new ArrayList<OverlayItem>();
		for (OverlayItem check : OverlaysFrom){
			if (check.getTitle().equals(marker)){
				OverlaysTo.add(check);
				toRemove.add(check);
			}
		}
		for (OverlayItem remove : toRemove){
			OverlaysFrom.remove(remove);
		}
	}*/
/*	public void filter_in(Drawable marker){
		for (OverlayItem check : mOverlaysStored){
			if (check.getMarker(0) == marker){
				mOverlays.add(check);
				mOverlaysStored.remove(check);
			}
		}
	}
	public void filter_out(Drawable marker){
		for (OverlayItem check : mOverlays){
			if (check.getMarker(0) == marker){
				mOverlaysStored.add(check);
				mOverlays.remove(check);
			}
		}
	}*/
	
	@Override
	protected OverlayItem createItem(int i)
	{
		try{
		return mOverlays.get(i);
		}catch(IndexOutOfBoundsException e){
			return null;
		}
	}

	@Override
	public int size()
	{
		return mOverlays.size();
	}
	@Override
	protected boolean onTap(int index)
	{
		if (mOverlays.isEmpty()){
			return false;
		}
		OverlayItem item = mOverlays.get(index);
		Toast.makeText(mContext,
				item.getTitle()+" "+ item.getSnippet(), 
				Toast.LENGTH_SHORT).show();
		return true;
	}
	@Override
	public boolean onTouchEvent(android.view.MotionEvent event,
                            MapView mapView){
		if (mOverlays.isEmpty()){
			return false;
		}
		else{
			return super.onTouchEvent(event, mapView);
		}
	}
}

